//
//  ZTextView.m
//
//  Created by ZhàoYǒng on 2018/1/5.
//  Copyright © 2018年 ZhàoYǒng. All rights reserved.
//

#import "ZTextView.h"
#import <Masonry/Masonry.h>
#import <ReactiveObjC/ReactiveObjC.h>

@interface ZTextView ()<UITextViewDelegate>

@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UILabel *plLabel;

@end

@implementation ZTextView

- (instancetype)init {
    self = [super init];
    if (self) {
        
        _textView = [[UITextView alloc] init];
        _textView.font = [UIFont systemFontOfSize:15.f];
        _textView.textColor = UIColor.blackColor;
        _textView.delegate = self;
        _textView.textContainerInset = UIEdgeInsetsZero;
        [self addSubview:_textView];
        [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        _plLabel = [[UILabel alloc] init];
        _plLabel.textColor = [UIColor colorWithRed:170.f/255 green:170.f/255 blue:170.f/255 alpha:1.f];;
        _plLabel.font = _textView.font;
        [self addSubview:_plLabel];
        [_plLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.textView);
            make.left.equalTo(self.textView).offset(3.f);
        }];
        
    }
    return self;
}

- (void)textViewDidChange:(UITextView *)textView {
    self.plLabel.hidden = textView.text.length > 0;
    if (self.textBlock) {
        self.textBlock(textView.text);
    }
}

- (NSString *)text {
    return _textView.text;
}

- (void)setText:(NSString *)text {
    _textView.text = text;
    if (self.textBlock) {
        self.textBlock(text);
    }
}

- (NSString *)placeholder {
    return _plLabel.text;
}

- (void)setPlaceholder:(NSString *)placeholder {
    _plLabel.text = placeholder;
}

- (UIFont *)font {
    return _textView.font;
}

- (void)setFont:(UIFont *)font {
    _textView.font = font;
}

- (UIColor *)textColor {
    return _textView.textColor;
}

- (void)setTextColor:(UIColor *)textColor {
    _textView.textColor = textColor;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    [super setBackgroundColor:backgroundColor];
    _textView.backgroundColor = backgroundColor;
}

@end
